package com.moco.launcher.activity;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.RingtoneManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.moco.launcher.R;
import com.moco.launcher.alarm.Constant;
import com.oazon.common.Assert;
import com.oazon.common.Logger;

public class RingActivity extends BaseActivity implements OnClickListener,
		OnItemClickListener {
	private static final String TAG = "RingActivity";
	
	private ImageButton mBtnComfirm, mBtnCancel;
	private ListView 	mLstViewRing;

	private String[] 	mAlarmTones;
	private String[] 	mAlarmTonePaths;
	private RingAdapter mRingAdapter;
	private ProgressBar mRingProgressBar;

	private Context 		mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.ring_list_layout);
		mContext = this;
		
		mRingProgressBar = (ProgressBar) findViewById(R.id.ring_progress_bar);
		mLstViewRing = (ListView)findViewById(R.id.ring_list_view);
		mBtnComfirm = (ImageButton)findViewById(R.id.ring_add_confirm);
		mBtnCancel = (ImageButton)findViewById(R.id.ring_cancel);

		mLstViewRing.setOnItemClickListener(this);
		mBtnComfirm.setOnClickListener(this);
		mBtnCancel.setOnClickListener(this);

		mRingProgressBar.setVisibility(View.VISIBLE);

		Logger.i(TAG, "before getAlarmTones");
		getAlarmTones();
	}
	
	private void getAlarmTones() {
		new AsyncTask<String, String, String>() {
			@Override
			protected String doInBackground(String... params) {
				Cursor alarmsCursor = null;
				try {
					RingtoneManager ringtoneMgr = new RingtoneManager(RingActivity.this);
					ringtoneMgr.setType(RingtoneManager.TYPE_ALARM);
					alarmsCursor = ringtoneMgr.getCursor();
					mAlarmTones = new String[alarmsCursor.getCount() + 1];
					mAlarmTones[0] = "Silent";
					mAlarmTonePaths = new String[alarmsCursor.getCount() + 1];
					mAlarmTonePaths[0] = "";
					Logger.i(TAG, "before getAlarmTones-----time is:" + System.currentTimeMillis());
					
					while (alarmsCursor.moveToNext()) {
						mAlarmTones[alarmsCursor.getPosition() + 1] = ringtoneMgr
								.getRingtone(alarmsCursor.getPosition())
								.getTitle(RingActivity.this);
						mAlarmTonePaths[alarmsCursor.getPosition() + 1] = ringtoneMgr
								.getRingtoneUri(alarmsCursor.getPosition())
								.toString();
					}
					
					Logger.i(TAG, "after getAlarmTones------time is:" + System.currentTimeMillis());
					Logger.d("AlarmAddActivity", "Finished Loading " + mAlarmTones.length + " Ringtones.");
				} catch (Exception e) {
					Logger.i("exception", e.toString());
					e.printStackTrace();
				} finally {
					if(alarmsCursor != null) {
						alarmsCursor.close();
					}
				}
				return null;
			}

			@Override
			protected void onPostExecute(String result) {
				super.onPostExecute(result);
				
				Logger.i(TAG, "enter  onPostExecute------time is:" + System.currentTimeMillis());
				
				mRingAdapter = new RingAdapter(mContext);
				mLstViewRing.setAdapter(mRingAdapter);
				mRingProgressBar.setVisibility(View.GONE);
			}
		}.execute("getAlarmTones");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ring_cancel:
			finish();
			break;
		case R.id.ring_add_confirm:
			break;
		}
	}

	class RingAdapter extends BaseAdapter {
		private LayoutInflater inflater;

		public RingAdapter(Context context) {
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			if (mAlarmTones != null)
				return mAlarmTones.length;
			
			return 0;
		}

		@Override
		public Object getItem(int position) {
			if (mAlarmTones != null)
				return mAlarmTones[position];
			
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = (View) inflater.inflate(R.layout.ring_item_lay, null);
				holder.textView = (TextView) convertView.findViewById(R.id.ring_item_txt);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			Assert.notNull(holder);
			holder.textView.setText(mAlarmTones[position]);
			return convertView;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent mIntent = new Intent();
		mIntent.putExtra("ring_uri", mAlarmTonePaths[position]);
		mIntent.putExtra("alarm_tone", mAlarmTones[position]);

		// 设置结果，并进行传送
		this.setResult(Constant.RESULT_CODE, mIntent);
		finish();
	}
	
	class ViewHolder {
		TextView textView;
	}
}